package com.wandian.setting.mvvm.usercenter.wlpagefragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.mvvm.BaseMVVMFragment;
import com.mylib.libcore.utils.UIUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wandian.setting.R;
import com.wandian.setting.adapter.UserInfoSDRecycleAdapter;
import com.wandian.setting.adapter.UserInfoWLRecycleAdapter;
import com.wandian.setting.databinding.SetFragmentSdPageBinding;
import com.wandian.setting.databinding.SetFragmentWlPageBinding;

/**
 * @author Administrator
 * @date 2019/12/16-13:30
 * @description RankingPageFragment
 **/
public class WLPageFragment extends BaseMVVMFragment<WLPageFragmentViewModel> {

    private SetFragmentWlPageBinding dataBinding;
    private UserInfoWLRecycleAdapter adapter;
    private String title = "围栏";
    private String user_id = "";
    private String user_name = "";
    private String user_head = "";
    private String user_des = "";
    private String form_id = "";
    private String isMine = "1";
    private int pageNum = 1;
    private int pageSize = 30;
    private UserInfoBean userInfoBean;
    public static WLPageFragment newInstance(String title, String user_id, String user_name, String user_head, String form_id) {
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("user_id", user_id);
        args.putString("user_name", user_name);
        args.putString("user_head", user_head);
        args.putString("form_id", form_id);
        WLPageFragment fragment = new WLPageFragment();
        fragment.setArguments(args);
        return fragment;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        dataBinding = SetFragmentWlPageBinding.inflate(inflater);
        dataBinding.setLifecycleOwner(this);
        return dataBinding.getRoot();
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        title = getArguments().getString("title")+"";
        user_id = getArguments().getString("user_id")+"";
        user_name = getArguments().getString("user_name")+"";
        user_head = getArguments().getString("user_head")+"";
        form_id = getArguments().getString("form_id")+"";
        if(!UIUtils.isEmpty(user_id) && !UIUtils.isEmpty(form_id) && user_id.equals(form_id)) isMine = "1";
        else  isMine = "0";
        userInfoBean = AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
        dataBinding.setViewModel(mViewModel);
        initViewData();
        getData(true);
    }

    private void getData(boolean isFlash){
        if(isFlash)pageNum=1;
        else pageNum = pageNum+1;

    }

    private void initViewData(){
        adapter = new UserInfoWLRecycleAdapter(R.layout.set_item_user_info_wl,isMine);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.recycle.setLayoutManager(layoutManager);
        dataBinding.recycle.setAdapter(adapter);
        adapter.setEmptyView(getLayoutInflater().inflate( R.layout.empty_view, null));
        dataBinding.smartRef.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh();
                refreshLayout.setEnableLoadMore(true);
            }
        });
        dataBinding.smartRef.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if(adapter != null && adapter.getData().size()>=pageSize && adapter.getData().size()%pageSize==0)getData(false);
                else refreshLayout.setEnableLoadMore(false);
                refreshLayout.finishLoadMore();
            }
        });

//        mViewModel.listSD.observe(this,listBean->{
//            if(!UIUtils.isEmpty(listBean)){
//                if(listBean.size()>=pageSize){
//                    adapter.setOnLoadMoreListener(this, dataBinding.recycle);
//                    adapter.setEnableLoadMore(true);
//                    adapter.loadMoreEnd(false);
//                }
//                else {
//                    adapter.setEnableLoadMore(false);
//                    adapter.loadMoreEnd(true);
//                }
//                if(pageNum==1){
//                    adapter.setNewData(listBean);
//                }else {
//                    adapter.addData(listBean);
//                    adapter.loadMoreComplete();
//                }
//            }
//        });
    }

}
